import { createModel } from "hox";
import { useState } from "react";
// import { useDebounceFn } from 'ahooks';
import { useGetRequest } from "../http";
import { getEntryInfo } from "../http/apis/intentional-api";

export interface EntryInfo {
    basic: any
    customer: any
    quote: any
    guarantorInfos: any
}

const useIntent = () => {

    const [entryInfo, setEntryInfo] = useState<EntryInfo>({
        basic: {},
        customer: {},
        quote: {},
        guarantorInfos: {},
    })
    const params = { url: getEntryInfo, successAction: (data: any) => setEntryInfo(data.content) }
    const fetchEntryInfo = useGetRequest(params).run
    const updateListData = (v: any, key: string, index: number, data: any, updatekey: string) => {
        const obj = data || {}
        const arr = obj[key] || []
        let list = [...arr]
        if (!list[index]) list[index] = {}
        Object.assign(list[index], { [updatekey]: v })
        return list
    }
    // const { run } = useDebounceFn((data: any, key: Group) => {
    //     const origin = entryInfo[key]
    //     setEntryInfo({ ...entryInfo, ...{ [key]: { ...origin, ...data } } })
    // }, {
    //     wait: 500,
    // })
    const updateCustomerInfo = (data: any) => {
        setEntryInfo({ ...entryInfo, ...{ customer: { ...entryInfo?.customer, ...data } } })
    }
    const updateGuarantorInfo = (data: any) => {
        setEntryInfo({ ...entryInfo, ...{ guarantorInfos: { ...entryInfo?.guarantorInfos, ...data } } })
    }

    return {
        entryInfo,
        fetchEntryInfo,
        updateCustomerInfo,
        updateGuarantorInfo,
        updateListData,
    }
}

export default createModel(useIntent)